package com.xhtt.modules.app.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.R;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.app.annotation.LoginUser;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.sys.entity.SysUserPosition;
import com.xhtt.modules.sys.service.SysUserPositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;

/**
 * <p>
 * 人员定位
 * </p>
 *
 * @author xj
 * @since 2019-05-09
 */
@RestController
@RequestMapping("app/sys/user/position")
@Api(value = "人员定位", tags = "人员定位")
public class SysUserPositionController {

    @Autowired
    private SysUserPositionService sysUserPositionService;

    @PostMapping(value = "/update")
    @ApiOperation("更新人员位置(app)")
    @Login
    public R update(@RequestBody SysUserPosition sysUserPosition, @ApiIgnore @LoginUser SysUserEntity user) {

        Integer userId = user.getUserId().intValue();
        // 是否存在这个用户的数据,已存在则修改,未存在新增
        SysUserPosition userPosition = sysUserPositionService.getOne(new LambdaQueryWrapper<SysUserPosition>().eq(SysUserPosition::getUserId, userId));
        if (userPosition != null) {
            userPosition.setPosX(sysUserPosition.getPosX());
            userPosition.setPosY(sysUserPosition.getPosY());
            sysUserPositionService.updateById(userPosition);
        } else {
            sysUserPosition.setUserId(userId);
            sysUserPositionService.save(sysUserPosition);
        }
        return R.ok();
    }

    @GetMapping(value = "/get")
    @ApiOperation("获取人员定位(web)")
    @Login
    public R get() {
        return R.ok().put(Constant.DATA, new ArrayList<>());
    }

}
